package com.yunyan.controller;


import com.yunyan.entity.SchoolData;
import com.yunyan.resp.JsonResp;
import com.yunyan.util.ExcelParser;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;

@RestController
public class UploadController {

    public static final String UPLOADED_FOLDER = System.getProperty("user.dir") + "/src/main/resources/static/";


    @PostMapping("/upload/resource")
    public JsonResp upload(@RequestParam("file") MultipartFile file) throws IOException {
        String name = saveFile(file);
        return JsonResp.ok(name);
    }

    @PostMapping("/upload/school")
    public JsonResp uploadSchool(@RequestParam("file") MultipartFile file) throws IOException {
        String name =  UUID.randomUUID().toString().replace("-", "") + "." + file.getOriginalFilename().split("[.]")[1];
        Path path = Paths.get(System.getProperty("user.dir") +"/学校/"  + name);
        // 真正的文件地址
        file.transferTo(path);
        // 返回文件名称
        return JsonResp.ok(name);
    }

    private String saveFile(MultipartFile file) throws IOException {
        // 生成新的文件名
        String name = "首页文档/" + UUID.randomUUID().toString().replace("-", "") + "." + file.getOriginalFilename().split("[.]")[1];
        Path path = Paths.get(UPLOADED_FOLDER  + name);
        // 真正的文件地址
        file.transferTo(path);
        return name;
    }
}
